define(["jquery","bbcare"], function ($,$bb) {
    var servers = {};
    var URL = $bb.baseUrl("nutrition");

    /**
     * 获取上一个页面传过来的参数
     */
    servers.getPageParams = function(){
        var params = $bb.getPageParams();
        return params.values;
    }

    /**
     * 请求营养方案详情
     */
    servers.ajaxSchemeDetails = function(params,index,callback){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition/schemeDetailQuery",
            data: JSON.stringify({
                data: params
            }),
            success: function (data){
                if(data.success){
                    callback(data.data,index,params);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "请求营养方案详情出错！");
                }
            }
        });
    };

    /**
     * 处理阶段数据
     */
    servers.handleStages = function(list){
        return {
            "list":list
        }
    }
    /**
     * 处理疗程数据
     */
    servers.handleStageCourses = function(list,stageId){
        var courses = [];
        for (var i = 0; i < list.length; i++) {
            if( list[i].stageId == stageId ){
                courses = list[i].courses;
            }
        }
        return {
            "list":courses
        }
    }

    /**
     * 添加营养方案阶段
     */
    servers.ajaxSchemeStagesModify = function(stageName,stageId,params,index,callback){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition/schemestage/modify",
            data: JSON.stringify({
                data: {
                    "schemeVersionId":params.schemeVersionId,
                    "stageName":stageName,
                    "stageId":stageId
                }
            }),
            success: function (data) {
                var str = stageId ? "修改" : "添加";
                if(data.success){
                    $bb.toastShow(str + "营养方案阶段成功！"); 
                    callback(index);
                }else{
                    $bb.toastShow(data.msg ? data.msg : str+"营养方案阶段出错！"); 
                }
            }
        });
    };

    /**
     * 删除营养方案阶段
     */
    servers.ajaxSchemeStagesDel = function(params){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition/schemeStageDel",
            data: JSON.stringify({
                data: {
                    "stageId":params.stageId
                }
            }),
            success: function (data) {
                if(data.success){
                    $bb.toastShow("删除该营养方案阶段成功！"); 
                    params.callback(params.index);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "删除该营养方案阶段出错！"); 
                }
            }
        });
    };

    /**
     * 移动营养方案阶段
     */
    servers.ajaxSchemeStagesMove = function(params,index,callback){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition/schemestage/ordermove",
            data: JSON.stringify({
                data: params
            }),
            success: function (data) {
                if(data.success){
                    $bb.toastShow("移动阶段成功！"); 
                    callback(index);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "移动营养方案阶段出错！"); 
                }
            }
        });
    };

    /**
     * 移动疗程
     */
    servers.ajaxStageCourseMove = function(params,index,callback){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition/stagecourse/ordermove",
            data: JSON.stringify({
                data: params
            }),
            success: function (data) {
                if(data.success){
                    $bb.toastShow("移动疗程成功！"); 
                    callback(index);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "移动营养方案阶段出错！"); 
                }
            }
        });
    };

    /**
     * 删除疗程
     */
    servers.ajaxStageCourseDel = function(params){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition/stageCourseDel",
            data: JSON.stringify({
                data: {
                    "stageCourseId":params.stageCourseId
                }
            }),
            success: function (data) {
                if(data.success){
                    $bb.toastShow("删除疗程成功！"); 
                    params.callback(params.index);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "删除疗程出错！"); 
                }
            }
        });
    };

    /**
     * 查询疗程库
     */
    servers.ajaxCourseLibiray = function(callback){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition/courselibiray/list",
            success: function (data) {
                if(data.success){
                    callback(data.rows);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "查询疗程库出错！");
                }
            }
        });
    };

    /**
     * 根据疗程库ID、疗程类型ID，查询疗程库菜单
     */
    servers.ajaxCourseMenus = function(libraryId,callback){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition/course/menulist",
            data: JSON.stringify({
                data: { 
                    "libraryId":libraryId 
                }
            }),
            success: function (data) {
                if(data.success){
                    callback(data.data);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "查询疗程库菜单出错！");
                }
            }
        });
    };

    /**
     * 添加疗程
     */
    servers.ajaxStageCourseAdd = function(params,index,callback){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition/stageCourseAdd",
            data: JSON.stringify({
                data: params
            }),
            success: function (data) {
                if(data.success){
                    $bb.toastShow("添加疗程成功！"); 
                    callback(index);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "添加疗程出错！"); 
                }
            }
        });
    }; 

    /**
     * 方案发布
     */
    servers.ajaxSchemePublish = function(schemeId){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition/schemePublish",
            data: JSON.stringify({
                data: {
                    "schemeId":schemeId
                }
            }),
            success: function (data) {
                if(data.success){
                    $bb.toastShow("方案发布成功！");
                }else{
                    $bb.toastShow(data.msg ? data.msg : "添加疗程出错！"); 
                }
            }
        });
    };

    /**
     * 方案删除
     */
    servers.ajaxSchemeDelete = function(params){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition/scheme/delete",
            data: JSON.stringify({
                data: {
                    "schemeId":params.schemeId
                }
            }),
            success: function (data) {
                if(data.success){
                    params.fn(data.data);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "方案删除出错！"); 
                }
            }
        });
    }; 

    return servers;
});